from SiemplifyAction import SiemplifyAction
from SiemplifyUtils import unix_now, convert_unixtime_to_datetime, output_handler
from ScriptResult import EXECUTION_STATE_COMPLETED, EXECUTION_STATE_FAILED, EXECUTION_STATE_TIMEDOUT
import requests
import json
import urllib


INTEGRATION_NAME = "SupernaZeroTrust"
SCRIPT_NAME = "Cyber Snapshot"

@output_handler
def main():
    siemplify = SiemplifyAction()
    
    url = "https://"
    apiroute = "/sera/v2/ransomware/criticalpaths"
    apitoken = siemplify.extract_configuration_param(provider_name=INTEGRATION_NAME, param_name="api_token")
    eyeglassip = siemplify.extract_configuration_param(provider_name=INTEGRATION_NAME, param_name="eyeglass_ip")
    eyeglassip = eyeglassip.replace('"', '')
    apitoken = apitoken.replace('"', '')
    siemplify.LOGGER.info("debug api token " + apitoken)
    siemplify.LOGGER.info("debug eyeglass ip " + eyeglassip)
    apicall = url + eyeglassip + apiroute
    siemplify.LOGGER.info("debug api route " + apicall)
    # siemplify.LOGGER.info("Current proxy configuration: {}", urllib.getproxies())

    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'api_key': apitoken
    }
    siemplify.LOGGER.info("Headers (Pretty Printed):")
    siemplify.LOGGER.info(json.dumps(headers, indent=4, sort_keys=True))

    response = None
    try:
        response = requests.post(apicall, headers=headers, timeout=30,verify=False)
        response.raise_for_status()  # This will raise an HTTPError for bad responses

        siemplify.LOGGER.info("\nSuccessful Response:")
        siemplify.LOGGER.info(response.text)
        status = EXECUTION_STATE_COMPLETED
        result_value = response.text
        output_message = "Eyeglass Zero Trust API Call Successful"

   
    except requests.exceptions.Timeout:
        siemplify.LOGGER.info("The request timed out")
        status = EXECUTION_STATE_TIMEDOUT
        result_value = "Timeout"
        output_message = "Eyeglass Zero Trust API Call Timed Out"

    except requests.exceptions.ConnectionError:
        siemplify.LOGGER.info("Connection error occurred")
        status = EXECUTION_STATE_FAILED
        result_value = "Connection Error"
        output_message = "Eyeglass Zero Trust API Call Connection Failed"

    except requests.exceptions.HTTPError as e:
        siemplify.LOGGER.info("HTTP error occurred: {0}".format(e))
        status = EXECUTION_STATE_FAILED
        result_value = "HTTP Error"
        output_message = "Eyeglass Zero Trust API Call HTTP Error: {0}".format(e)

    except requests.exceptions.RequestException as e:
        siemplify.LOGGER.info("An error occurred: {0}".format(e))
        status = EXECUTION_STATE_FAILED
        result_value = "Request Error"
        output_message = "Eyeglass Zero Trust API Call Error: {0}".format(e)

    siemplify.LOGGER.info("\nstatus: {0}\nresult_value: {1}\noutput_message: {2}".format(status, result_value, output_message))
    siemplify.end(output_message, result_value, status)

if __name__ == "__main__":
    main()
